package com.netty.esc.service.action;

import com.alibaba.fastjson.JSONObject;
import com.netty.esc.utils.ContextUtil;
import com.netty.esc.utils.StringUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

/**
 * @Author: Etan96
 * @CreateDate: 2022/8/29 20:10
 * @Description: 任务执行器
 */
@Slf4j
public class ActionProcessor {
    /**
     * 标注{@link ActionType TaskKey}注解的方法
     */
    private final String methodName;

    private final String className;

    public ActionProcessor(Class<?> clazz, Method method) {
        this.className = StringUtil.StringToMin(clazz.getSimpleName());
        this.methodName = method.getName();
    }

    void execute(ChannelHandlerContext context, JSONObject jsonObject) {
        try {
            Object object = ContextUtil.getBean(className);
            object.getClass()
                    .getDeclaredMethod(methodName, ChannelHandlerContext.class, JSONObject.class)
                    .invoke(object, context, jsonObject);
        } catch (Throwable throwable) {
            log.error("任务调度发生异常", throwable);
        }
    }
}
